package com.westos.userauth.web;

import com.westos.userauth.domain.UserLogin;
import com.westos.userauth.domain.UserLoginSecurityGroup;
import com.westos.userauth.dto.SecurityPermissionDto;
import com.westos.userauth.domain.SecurityGroup;
import com.westos.userauth.domain.SecurityPermission;
import com.westos.userauth.domain.UserLoginSecurityGroup;
import com.westos.userauth.domain.UserLoginSecurityQuestion;
import com.westos.userauth.service.Security;
import com.westos.userauth.service.impl.SecurityImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.westos.userauth.domain.SecurityGroup;
import com.westos.userauth.domain.SecurityPermission;
import com.westos.userauth.domain.UserLoginSecurityQuestion;
import io.swagger.annotations.ApiOperation;

import java.util.List;


@RestController
@RequestMapping("/api/security")
public class SecurityController {
    @Autowired
    private Security security;


    //添加权限到权限组   (liuhao)
    @ApiOperation("添加权限到权限组")
    @PostMapping("/addSecurityPermissionToSecurityGroup")
    public ResponseEntity<String> addSecurityPermissionToSecurityGroup(SecurityPermissionDto securityPermissionDto) {
        boolean flag = security.addSecurityPermissionToSecurityGroup(securityPermissionDto);
        if (flag) {
            return new ResponseEntity<String>("添加权限到权限组成功", HttpStatus.OK);
        }
        return new ResponseEntity<String>("添加权限到权限组失败", HttpStatus.BAD_REQUEST);
    }

    //添加用户登录到权限组  (liuhao)
    @ApiOperation("添加用户登录到权限组")
    @PostMapping("/addUserLoginToSecurityGroup")
    public ResponseEntity<String> addUserLoginToSecurityGroup(UserLoginSecurityGroup userLoginSecurityGroup) {
        int flag = security.addUserLoginToSecurityGroup(userLoginSecurityGroup);
        if (flag > 0) {
            return new ResponseEntity<String>("添加权限组成功", HttpStatus.OK);
        }
        return new ResponseEntity<String>("添加权限组失败", HttpStatus.BAD_REQUEST);
    }

    //创建新的权限组   （liuhao）
    @ApiOperation("创建权限组")
    @PostMapping("/createSecurityGroup")
    public ResponseEntity<String> createSecurityGroup(@RequestParam("groupId") String groupId, @RequestParam("description") String description){
        int flag = security.createSecurityGroup(groupId, description);
        if (flag>0){
            return new ResponseEntity<String>("创建权限组成功",HttpStatus.OK);
        }
        return new ResponseEntity<String>("创建权限组失败",HttpStatus.BAD_REQUEST);
    }
    //创建用户登录(liuhao)
    @ApiOperation("创建用户登录")
    @PostMapping("/createUserLogin")
    public ResponseEntity<String> createUserLogin(UserLogin userLogin){
        int flag = security.createUserLogin(userLogin);
        if (flag>0){
            return new ResponseEntity<String>("创建用户登录成功",HttpStatus.OK);
        }
        return  new ResponseEntity<String>("创建用户登录失败",HttpStatus.BAD_REQUEST);
    }

    // 根据用户名查找用户的权限组（方政）
    @ApiOperation("根据用户名查找用户的权限组")
    @GetMapping("/findUserLoginSecurityGroupByUserLoginId")
    public ResponseEntity<List> findUserLoginSecurityGroupByUserLoginId(String userLoginId){
        List<UserLoginSecurityGroup> userLoginSecurityGroupByUserLoginId = security.findUserLoginSecurityGroupByUserLoginId(userLoginId);
        return new ResponseEntity<List>(userLoginSecurityGroupByUserLoginId, HttpStatus.OK);
    }
    //权限是否在权限组中（方政）
    @ApiOperation("权限是否在权限组中")
    @GetMapping("/securityGroupPermissionExists")
    public ResponseEntity<Boolean> securityGroupPermissionExists(String permissionId,String groupId){
        Boolean b = security.securityGroupPermissionExists(permissionId, groupId);
        return new ResponseEntity<Boolean>(b, HttpStatus.OK);
    }
    //是否有权限（方政）
    @ApiOperation("是否有权限")
    @GetMapping("/hasPermission")
    public ResponseEntity<Boolean> hasPermission(String loginId,String permissionId){
        Boolean b = security.hasPermission(loginId, permissionId);
        return new ResponseEntity<Boolean>(b, HttpStatus.OK);


    }

    @ApiOperation("修改权限组")
    @GetMapping("/updateSecurityGroup")
    public ResponseEntity<Integer> updateSecurityGroup(SecurityGroup securityGroup){
        int i = security.updateSecurityGroup(securityGroup);
        if (i>0) {
            return new ResponseEntity<>(i, HttpStatus.OK);
        }
        return new ResponseEntity<>(i,HttpStatus.BAD_REQUEST);
    }

    @ApiOperation("创建权限")
    @GetMapping("/createSecurityPermission")
    public ResponseEntity<Integer> createSecurityPermission(SecurityPermission securityPermission){
        int i = security.createSecurityPermission(securityPermission);
        if (i>0){
            return new ResponseEntity<>(i, HttpStatus.OK);
        }
        return new ResponseEntity<>(i,HttpStatus.BAD_REQUEST);
    }

    @ApiOperation("修改权限")
    @GetMapping("updateSecurityPermission")
    public ResponseEntity<Integer> updateSecurityPermission(SecurityPermission securityPermission){
        int i = security.updateSecurityPermission(securityPermission);
        if (i>0){
            return new ResponseEntity<>(i,HttpStatus.OK);
        }
        return new ResponseEntity<>(i,HttpStatus.BAD_REQUEST);
    }

    @ApiOperation("从权限组中移除权限")
    @GetMapping("/removeSecurityPermissionFromSecurityGroup")
    public ResponseEntity<Boolean> removeSecurityPermissionFromSecurityGroup(@RequestParam("permissionId") String permissionId, @RequestParam("groupId") String groupId){
        boolean b = security.removeSecurityPermissionFromSecurityGroup(permissionId, groupId);

        if (b){
            return new ResponseEntity<>(b,HttpStatus.OK);
        }

        return new ResponseEntity<>(b,HttpStatus.BAD_REQUEST);
    }


    //修改用户安全提示问题 王实
    @ApiOperation("修改用户安全提示问题")
    @GetMapping("/updateUserLoginSecurityQuestion")
    public ResponseEntity<String> updateUserLoginSecurityQuestion(@ModelAttribute("question")UserLoginSecurityQuestion question){
        Boolean aBoolean = security.updateUserLoginSecurityQuestion(question);
        if (aBoolean) {
            return new ResponseEntity<String>("更新成功", HttpStatus.OK);
        }
        return new ResponseEntity<String>("更新失败",HttpStatus.BAD_REQUEST);
    }

    //删除用户登录的安全提示问题（王实）
    @ApiOperation("删除用户登录的安全提示问题")
    @GetMapping("/removeUserLoginSecurityQuestion")
    public ResponseEntity<String> removeUserLoginSecurityQuestion(@RequestParam("userLoginId")String userLoginId, @RequestParam("questionEnumId")String questionEnumId){
        UserLoginSecurityQuestion userLoginSecurityQuestion = new UserLoginSecurityQuestion();
        userLoginSecurityQuestion.setUserLoginId(userLoginId);
        userLoginSecurityQuestion.setQuestionEnumId(questionEnumId);
        Boolean aBoolean = security.removeUserLoginSecurityQuestion(userLoginSecurityQuestion);
        if (aBoolean) {
            return new ResponseEntity<String>("删除成功", HttpStatus.OK);
        }
        return new ResponseEntity<String>("删除失败",HttpStatus.BAD_REQUEST);
    }

    //删除权限组（王实）
    @ApiOperation("删除权限组")
    @GetMapping("/deleteSecurityGroup")
    public ResponseEntity<String> deleteSecurityGroup(@RequestParam("groupId")String groupId){
        Boolean aBoolean = security.deleteSecurityGroup(groupId);
        if (aBoolean) {
            return new ResponseEntity<String>("删除权限组成功", HttpStatus.OK);
        }
        return new ResponseEntity<String>("删除权限组失败",HttpStatus.BAD_REQUEST);
    }
    //修改用户登陆(王妍)
    @ApiOperation("修改用户登陆")
    @GetMapping("/updateUserLogin")
    public ResponseEntity<Boolean> updateUserLogin(UserLogin userLogin){
        Boolean flag = security.updateUserLogin(userLogin);
        if(flag){
            return new ResponseEntity<>(flag,HttpStatus.OK);
        }else{
            return new ResponseEntity<>(flag,HttpStatus.BAD_REQUEST);
        }

    }
    //修改用户的密码(王妍)
    @ApiOperation("修改用户的密码")
    @GetMapping("/updateUserLoginPassword")
    public ResponseEntity<Boolean>updateUserLoginPassword(String userLoginId,String currentPassword,String newpassword){
        Boolean flag = security.updateUserLoginPassword(userLoginId, currentPassword, newpassword);
        if(flag){
            return new ResponseEntity<>(flag,HttpStatus.OK);
        }else{
            return new ResponseEntity<>(flag,HttpStatus.BAD_REQUEST);
        }
    }
    @PostMapping("/updateUserLoginToSecurityGroup")
    public ResponseEntity<Boolean> updateUserLoginToSecurityGroup(@ModelAttribute UserLoginSecurityGroup userLoginSecurityGroup){
        return new ResponseEntity<>(security.updateUserLoginToSecurityGroup(userLoginSecurityGroup), HttpStatus.OK);
    }
    @PostMapping("/removeUserLoginToSecurityGroup")
    public ResponseEntity<Boolean> removeUserLoginToSecurityGroup(@ModelAttribute UserLoginSecurityGroup userLoginSecurityGroup){
        return new ResponseEntity<>(security.removeUserLoginToSecurityGroup(userLoginSecurityGroup), HttpStatus.OK);
    }
    @PostMapping("/createUserLoginSecurityQuestion")
    public ResponseEntity<Boolean> createUserLoginSecurityQuestion(@ModelAttribute UserLoginSecurityQuestion userLoginSecurityQuestion){
        return new ResponseEntity<>(security.createUserLoginSecurityQuestion(userLoginSecurityQuestion), HttpStatus.OK);
    }
    public SecurityController() {
    }
    public SecurityController(Security security) {
        this.security=security;
    }

}
